<?php
/**
 *	Home Actions.
 *	@package		mv2.action.account
 *	@extends		Framework_Krypton_Core_DefinitionAction
 *	@uses			Logic_User
 *	@uses			Logic_Role
 *	@uses			Logic_Authentication
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			29.08.2007
 *	@version		0.1
 */
/**
 *	Home Actions.
 *	@package		mv2.action.account
 *	@extends		Framework_Krypton_Core_DefinitionAction
 *	@uses			Logic_User
 *	@uses			Logic_Role
 *	@uses			Logic_Authentication
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			29.08.2007
 *	@version		0.1
 */
class Action_Administration_User extends Framework_Krypton_Core_DefinitionAction
{
	/**
	 *	Constructor.
	 *	@access		public
	 *	@return		void
	 */
	function __construct()
	{
		$request = Framework_Krypton_Core_Registry::getStatic('request');
		parent::__construct();
		$this->loadLanguage( 'administration.user','user' );
        $this->addAction( 'addUser' );
        $this->addAction( 'editUser' );
        $this->addAction( 'editStatus' );
        $this->addAction( 'addUserRoles' );
        $this->addAction( 'removeUserRoles' );
        $this->addAction( 'resetFilters' );

	}

	public function act()
	{
		$this->filterUsers();
	}

    protected function addUser()
    {
		$request	= $this->registry->get( 'request' );
		$data		= $request->getAll();
		$logic		= new Logic_User();
		try
		{
			$data['creatorId']	= Logic_Authentication::getCurrentUser();
			$userId = $logic->addUser( $data );
			$this->messenger->noteSuccess($this->words['user']['msg']['success_user_added']);
			$this->restart( "?link=".$request->get( 'link' ) . '&userId=' . $userId );
		}
		catch( Exception $e )
		{
			$this->handleException( $e, 'user','addUser' );
		}
    }

	protected function addUserRoles()
	{
		$request	= $this->registry->get( 'request' );
		$session	= $this->registry->get( 'session' );
		$userId		= $request->get( 'userId' );
		$roles		= $request->get( 'roles_add' );

		//neuer user
		if( $request->has( 'add' ) )
		{
			$setRoles	= $session->get( 'add_user_roles' );
			foreach( $roles as $roleId )
			{
				if( ( $key = array_search( $roleId, $setRoles ) ) === FALSE )
				{
					$setRoles[] = $roleId;
				}
			}
			$session->set( 'add_user_roles', $setRoles );
		}
		//rolleneditor
		else
		{
			foreach( $roles as $roleId )
			{
				try
				{
					Logic_Role::addRoleUser( $roleId, $userId );
				}
				catch( Exception $e )
				{
					$this->handleException( $e, 'user','editUserRoles' );
				}
			}
		}
	}

	protected function editStatus()
	{
		$request	= $this->registry->get( 'request' );
		$userId		= $request->get( 'userId' );
		$status		= $request->get( 'set_status' );
		Logic_User::setStatus( $userId, $status );
		//  send mail
		//  message
	}

    protected function editUser()
    {
		$request	= $this->registry->get( 'request' );
		$data		= $request->getAll();
		$userId		= $request->get( 'userId' );
		$logic		= new Logic_User();
		try
		{
			if( $logic->editUser( $userId, $data ) )
				$this->messenger->noteSuccess($this->words['user']['msg']['success_user_edited']);
			else
				$this->messenger->noteSuccess($this->words['user']['msg']['error_user_not_edited']);
			$this->restart( "?link=".$request->get( 'link' ) );
		}
		catch( Exception $e )
		{
			$this->handleException( $e, 'user','editUser' );
		}
    }

    /**
	 *	Sets current Filters in Session.
	 *	@access		protected
	 *	@return		void
	 */
	protected function filterUsers()
	{
		$config		= $this->registry->get( 'config' );
		$session	= $this->registry->get( 'session' );
		$request	= $this->registry->get( 'request' );
 		if( $request->get( 'filterUsers' ) )
		{
			$session->set( 'account_users_user_id',		$request->get( 'filter_user_id' ) );
			$session->set( 'account_users_username',	$request->get( 'filter_username' ) );
			$session->set( 'account_users_email',		$request->get( 'filter_email' ) );
			$session->set( 'account_users_order',		$request->get( 'filter_order' ) );
			$session->set( 'account_users_direction',	$request->get( 'filter_direction' ) );
			$session->set( 'account_users_limit',		$request->get( 'filter_limit' ) );
			$session->set( 'account_users_offset',		0 );
		}
		else
		{
			if( NULL !== ( $offset = $request->get( 'offset' ) ) )
				$session->set( 'account_users_offset', $offset );
			if( !$session->get( 'account_users_limit' ) )
				$session->set( 'account_users_limit', $config['layout.list_limit'] );
		}
		if( !$session->get( 'account_users_limit' ) )
			 $session->set( 'account_users_limit', $config['layout.list_limit'] );
		if( $session->get( 'account_users_offset' ) < 0 )
			$session->set( 'account_users_offset', 0 );
	}

	protected function removeUserRoles()
	{
		$request	= $this->registry->get( 'request' );
		$session	= $this->registry->get( 'session' );
		$userId		= $request->get( 'userId' );
		$roles		= $request->get( 'roles_remove' );

		//neuer user
		if( $request->has( 'add' ) )
		{
			$setRoles	= $session->get( 'add_user_roles' );
			foreach( $roles as $roleId )
			{
				if( ( $key = array_search( $roleId, $setRoles ) ) !== FALSE )
				{
					unset( $setRoles[$key] );
				}
			}
			$session->set( 'add_user_roles', $setRoles );
		}
		//rolleneditor
		else
		{
			foreach( $roles as $roleId )
			{
				try
				{
					Logic_Role::removeRoleUser( $roleId, $userId );
				}
				catch( Exception $e )
				{
					$this->handleException( $e, 'user','editUserRoles' );
				}
			}
		}
	}
	protected function resetFilters()
	{
		$session	= $this->registry->get( 'session' );
		$session->remove( 'account_users_user_id' );
		$session->remove( 'account_users_username' );
		$session->remove( 'account_users_email' );
		$session->remove( 'account_users_order' );
		$session->remove( 'account_users_direction' );
		$session->remove( 'account_users_limit' );
		$session->remove( 'account_users_offset' );
	}
}
?>